package com.zzhardy.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.layout.font.FontProvider;
import org.apache.commons.io.IOUtils;

public class PdfUtils {
    
    /** pdf加载的字体目录 */
    private static String FONT_PATH = "/fonts";
    
    
    public static void main(String[] args) throws Exception {
        PdfUtils pdfUtils   = new PdfUtils();
        String              pathUrl = UrlUtils.getMavenChildPath(new PdfUtils()) + "generator/pdf/";
        File file = new File(pathUrl);
        if (!file.exists()) {
            file.mkdirs();
        }
        String   filePath   = pathUrl + "TestHtm3232121.pdf";
        Map<String, Object> beanMap = new HashMap<String, Object>();
        beanMap.put("fileName", "User");// 实体类名
        beanMap.put("interfaceName", "User");// 接口名
        List<Map<String, String>> paramsList = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 4; i++) {
            Map<String, String> tmpParamMap = new HashMap<String, String>();
            tmpParamMap.put("fieldName", "field中d文Name" + i);
            tmpParamMap.put("fieldNote", "中d文fieldNote" + i);
            tmpParamMap.put("fieldType", "中d文String");
            paramsList.add(tmpParamMap);
        }
        beanMap.put("params", paramsList);
        pdfUtils.createPdf("test/html/image.html", beanMap, filePath);
        //pdfUtils.downCreatePdf("test/html/image.html", beanMap);
    }
    
    /**
     * 生成指定的Pdf在指定目录下
     */
    public String createPdf(String templateUrl, Map<String, Object> paras, String filePath) {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        URL          url                = contextClassLoader.getResource(templateUrl);
        String fileUrl = url.getFile();
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(1024)) {
            String htmlStr = FreeMakerUtils.templateToStr("", paras, templateUrl);
            //导入字体
            FontProvider fontProvider = new FontProvider();
            try {
                fontProvider.addFont(IOUtils.toByteArray(contextClassLoader.getResourceAsStream(FONT_PATH + "/yahei.ttf")));
                fontProvider.addFont(IOUtils.toByteArray(contextClassLoader.getResourceAsStream(FONT_PATH + "/yaheiBold.ttf")));
            } catch (Exception e) {
                fontProvider.addSystemFonts();
            }
            
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setFontProvider(fontProvider);
            converterProperties.setCharset("utf-8");
            HtmlConverter.convertToPdf(htmlStr, bos, converterProperties);
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(bos.toByteArray());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    
    /**
     * 下载pdf
     * @param templateUrl
     * @param paras
     * @return
     */
    public static ByteArrayOutputStream downCreatePdf(String templateUrl, Map<String, Object> paras) {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(1024)) {
            String htmlStr = FreeMakerUtils.templateToStr("", paras, templateUrl);
            //导入字体
            FontProvider fontProvider = new FontProvider();
            try {
                ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
                fontProvider.addFont(IOUtils.toByteArray(contextClassLoader.getResourceAsStream(FONT_PATH + "/yahei.ttf")));
                fontProvider.addFont(IOUtils.toByteArray(contextClassLoader.getResourceAsStream(FONT_PATH + "/yaheiBold.ttf")));
            } catch (Exception e) {
                fontProvider.addSystemFonts();
            }
            
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setFontProvider(fontProvider);
            converterProperties.setCharset("utf-8");
            HtmlConverter.convertToPdf(htmlStr, bos, converterProperties);
            return bos;
        } catch (Exception e) {
        }
        return new ByteArrayOutputStream(1024);
    }
}
